package com.study.animationstudy.widget

import android.view.animation.Animation
import android.view.animation.Transformation

/**
 *
 * @Author： LJH
 * @Time： 2022/12/16
 * @description：
 */
class SwingAnimation(
    private val middleDegrees:Float,
    private val leftDegrees:Float,
    private val rightDegrees:Float,
    private val pivotXType:Int,
    private val pivotXValue:Float,
    private val pivotYType:Int,
    private val pivotYValue:Float):Animation() {
    // 中间的角度
    var mMiddleDegrees = 0f
    //左边的角度
    var mLeftDegrees = 0f
    //右边的角度
    var mRightDegrees = 0f
    //圆心的横坐标类型
    var mPivotXType = 0
    //圆心的纵坐标类型
    var mPivotYType = 0
    // 圆心横坐标的数值比例
    var mPivotXValue = 0f
    // 圆心纵坐标的数值比例
    var mPivotYValue = 0f
    // 圆心横坐标的数值
    var mPivotX = 0f
    // 圆心纵坐标的数值
    var mPivotY = 0f
    init {
        mMiddleDegrees = middleDegrees
        mLeftDegrees = leftDegrees
        mRightDegrees = rightDegrees
        mPivotXValue = pivotXValue
        mPivotXType = pivotXType
        mPivotYValue = pivotYValue
        mPivotYType = pivotYType
        initializePivotPoint()
    }

    constructor(middleDegrees: Float, leftDegrees: Float, rightDegrees: Float)
            : this(middleDegrees, leftDegrees, rightDegrees, 0.0f, 0.0f)

    constructor(
        middleDegrees: Float, leftDegrees: Float,
        rightDegrees: Float, pivotX: Float, pivotY: Float
    ) : this(middleDegrees, leftDegrees, rightDegrees, ABSOLUTE, pivotX, ABSOLUTE, pivotY)

    /**
     * 初始化圆心的横纵坐标数值
     */
    private fun initializePivotPoint(){
        if (mPivotXType == ABSOLUTE) {
            mPivotX = mPivotXValue
        }
        if (mPivotYType == ABSOLUTE) {
            mPivotY = mPivotYValue
        }
    }

    /**
     * 在动画变换过程中调用
     * @param interpolatedTime Float
     * @param t Transformation
     */
    override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
        var degrees = 0f
        // 摆到左边端点时的时间比例
        val leftPos = (1f/4f)
        // 摆到右边端点时的时间比例
        val rightPos = (3f/4f)
        when{
            // 从中间线往左边端点摆
            interpolatedTime <= leftPos -> {
                degrees = mMiddleDegrees + ((mLeftDegrees - mMiddleDegrees) * interpolatedTime * 4)
            }
            // 从左边端点往右边端点摆
            interpolatedTime > leftPos && interpolatedTime < rightPos -> {
                // 从左边端点往右边端点摆
                degrees = mLeftDegrees + (mRightDegrees - mLeftDegrees) * (interpolatedTime - leftPos) * 2
            }
            // 从右边端点往中间线摆
            else -> {
                // 从右边端点往中间线摆
                degrees = mRightDegrees + (mMiddleDegrees - mRightDegrees) * (interpolatedTime - rightPos) * 4
            }
        }
        //获得缩放比率
        val scale = scaleFactor
        if (mPivotX == 0.0f && mPivotY == 0.0f){
            t?.matrix?.setRotate(degrees)
        } else {
            t?.matrix?.setRotate(degrees,mPivotX * scale,mPivotY * scale)
        }
    }

    /**
     * 在初始化时调用
     * @param width Int
     * @param height Int
     * @param parentWidth Int
     * @param parentHeight Int
     */
    override fun initialize(width: Int, height: Int, parentWidth: Int, parentHeight: Int) {
        super.initialize(width, height, parentWidth, parentHeight)
        mPivotX = resolveSize(mPivotXType,mPivotXValue,width,parentWidth)
        mPivotY = resolveSize(mPivotYType,mPivotYValue,height,parentHeight)
    }
}